package com.web.biz.common;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Element;

import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.platform.web.controller.BaseDomainCtrl;
import com.css.sword.platform.web.controller.annotations.CTRL;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.sun.org.apache.xpath.internal.XPathAPI;
import com.web.common.utils.DateFormatUtil;
import com.web.common.utils.FileDomainConfig;
import com.web.common.utils.FileUploadUtiles;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.ParamConfig;
import com.web.common.utils.XMLAdapter;
import com.web.common.utils.Zxing.MatrixToImageWriter;


@CTRL("ZxingCtrl")
public class ZxingCtrl extends BaseDomainCtrl {

	private static SwordLogUtils log = SwordLogUtils.getLogger(ZxingCtrl.class);

	/**
	 * 生成二维码并返回二维码路径
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public IResData genQRcodeAndGetPath(IReqData req) throws Exception{

		log.debug("开始进入 ZxingCtrl_genQRcodeAndGetPath 方法。");

		IResData res = new SwordRes();
		String flag= "false";
		try {
            String username=LoginInfoUtil.getLoginUserName();
            if ("".equals(username)) {
            	res.addAttr("flag", flag);
				return res;
			}
            //二维码图片对应的信息（亦是文件名）
		    String content = username+System.currentTimeMillis();
		     
		    Element element = FileDomainConfig.getInstance().getOrgElement("share");
			System.out.println("上传文件："+XMLAdapter.nodeXML(element));
			Element domainElement = (Element) XPathAPI.selectSingleNode(element, "FileDomainList/FileDomain[@Name='"+FileUploadUtiles.DOMAIN_QRCODE+"']");
			String domainpath = domainElement.getAttribute("Path");
			String path = username+"/"+DateFormatUtil.getCurrentYear()+"/"+DateFormatUtil.getCurrentMonth()+"/"+DateFormatUtil.getCurrentDate();
			String filepath = domainpath.substring(0,domainpath.length()).replace("\\", "/")+"/"+path+"/";
			File desPath =new File(filepath);
			//如果文件夹不存在则创建
			if  (!desPath.exists())
			{
				desPath.mkdirs();//.mkdir();
			}
		    //真正生成二维码的逻辑
		    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		     
		    Map hints = new HashMap();
		    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		    BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 800,800,hints);
		    String fileName=content+".jpg";
		    File file1 = new File(filepath,fileName);
		    
		    //要返回的路径
		    String webPath=FileUploadUtiles.DOMAIN_QRCODE+"/"+path+"/"+fileName;
		    String webFullPath=ParamConfig.getInstanse().getParam("fileDomainPath")+webPath;
		    if (MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1)) {
		    	flag = "true";
		    	res.addAttr("webPath", webPath);
		    	res.addAttr("webFullPath", webFullPath);
		    	res.addAttr("info", content);
		    }
		 } catch (Exception e) {
		     e.printStackTrace();
		 }
		
		res.addAttr("flag", flag);
		return res;

	}

	 		


}